<?php

class CommonController extends Controller
{
    public function init()
    {
        if ($userinfo = M('user')->checklogin()) {
            define('IS_LOGIN', true);
            $this->userinfo = $userinfo;
        } else {
            define('IS_LOGIN', false);
        }
        $this->assign('islogin', IS_LOGIN);
        $this->categorysign = '';
        $this->navsign = '';
    }

    public function listAction()
    {
        $key = I('get.key', 'en', I('get.module', 'en', 'news'));
        $page = I('get.page', 'int', 1);
        $pagesize = C("num_" . CONTROLLER_NAME, null, 10);
        $categoryInfo = M('category')->field('id,name,level,keywords,description,listtpl,infonum')->where(array('key' => $key))->find();
        if ($categoryInfo) {
            $sonCategoryId = M('category')->getSonId($categoryInfo['id']);
            if (!empty($sonCategoryId)) $where['category_id'] = array('in', $sonCategoryId);
            $where['status'] = 1;
            $this->isfinal = (count($sonCategoryId) == 1) ? true : false;
            $this->position = M('category')->getPosition($categoryInfo['id']);
            $this->categorylist = M('category')->getSiblingList($categoryInfo['id']);
            $this->categorysign = $key;
            $this->categoryid = $categoryInfo['id'];
            $this->categoryname = $categoryInfo['name'];
            $this->categoryurl = M('category')->geturl($categoryInfo['id']);
            $this->navsign = $this->position['asc']['0']['key'];
            $this->totalnum = $categoryInfo['infonum'];
            $this->list = M('archive')->where($where)->limit($pagesize)->page($page)->order('top desc,id desc')->getlist('title,category_id,key,module,updatetime,commentnum,viewnum,description,litpic');
            $this->page = $this->showpage($this->totalnum, $pagesize, $page, M('category')->geturl($categoryInfo['id'], '__PAGE__'));
            $this->keywords = $categoryInfo['keywords'];
            $this->description = $categoryInfo['description'];
            $this->display($categoryInfo['listtpl']);
        } else {
            pt::err404(M('category')->getlastsql() . M('category')->geterror() . '找不到您要访问的栏目');
        }
    }

    public function detailAction()
    {
        $key = I('get.key', 'en', '');
        $info = M('archive')->getinfo($key, 'key');
        if ($info) {
            $this->info = $info;
            $this->position = M('category')->getPosition($info['category_id']);
            $this->navsign = $this->position['asc']['0']['key'];
            $this->categorylist = M('category')->getSiblingList($info['category_id']);
            $this->categorysign = $info['category_key'];
            $this->preinfo = M('archive')->getpre($info['id'], $info['category_id']);
            $this->nextinfo = M('archive')->getnext($info['id'], $info['category_id']);
            $this->commentlist = M('comment')->where(array('aid' => $info['id'], 'status' => 1))->order('id desc')->select();
            M('archive')->where(array('id'=>$info['id']))->setinc('viewnum');
            $this->display();
        } else {
            pt::err404(M('archive')->getlastsql() . M('archive')->geterror() . '找不到指定的文档');
        }
    }

    public function showpage($totalnum, $perpage, $nowpage, $url)
    {
        $totalpage = ceil($totalnum / $perpage);
        if ($nowpage > $totalpage) $nowpage = $totalpage;
        if ($totalpage == 1) return '';
        if ($totalpage < 6) {
            $start = 1;
            $end = $totalpage;
        } elseif ($nowpage < 3) {
            $start = 1;
            $end = 5;
        } elseif ($totalpage < $nowpage + 2) {
            $start = $totalpage - 4;
            $end = $totalpage;
        } else {
            $start = $nowpage - 2;
            $end = $nowpage + 2;
        }
        if ($totalpage < 1) return '';
        $str = "";
        if ($nowpage > 1) {
            if (strpos($url, '/__PAGE__')){
                $str .= '<li><a class="page-numbers" href="' . str_replace('/__PAGE__', '', $url) . '">首页</a></li> ';
            }else{
                $str .= '<li><a class="page-numbers" href="' . str_replace('__PAGE__', 1, $url) . '">首页</a></li> ';
            }
            if ($nowpage == 2 && strpos($url, '/__PAGE__')) {
                $str .= '<li><a class="page-numbers" href="' . str_replace('/__PAGE__', '', $url) . '">上一页</a></li> ';
            } else {
                $str .= '<li><a class="page-numbers" href="' . str_replace('__PAGE__', ($nowpage - 1), $url) . '">上一页</a></li> ';
            }
        }
        for ($i = $start; $i <= $end; $i++) {
            if ($i == $nowpage) {
                $str .= '<li class="page-numbers current"><a id="current_page_number" href="javascript:void(0);">' . $i . '</a></li> ';
            } else {
                if ($nowpage == 2 && strpos($url, '/__PAGE__')) {
                    $tmp = str_replace('/__PAGE__', '', $url);
                } else {
                    $tmp = str_replace('__PAGE__', $i, $url);
                }
                $str .= '<li><a class="page-numbers" href="' . $tmp . '">' . $i . '</a></li> ';
            }
        }
        if ($nowpage == $totalpage) {
        } else {
            $str .= '<li><a class="page-numbers" href="' . str_replace('__PAGE__', ($nowpage + 1), $url) . '" >下一页</a></li> ';
            $str .= ' <li><a class="page-numbers" href="' . str_replace('__PAGE__', $totalpage, $url) . '">尾页</a></li> ';
        }
        return $str;
    }
}